<?php
/**
 * Framework_OpenID_Consumer
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package OpenID
 * @filesource
 */

require_once "Auth/OpenID/Consumer.php";

/**
 * Framework_OpenID_Consumer
 *
 * This is the base class for user related functions
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @package OpenID
 */

class Framework_OpenID_Consumer extends Framework_Object
{

	/**
     * $store
     *
     * @access      private
     * @var         object		$store      OpenID store
     */
	private $store;

	/**
     * $consumer
     *
     * @access      private
     * @var         object		$consumer      OpenID consumer
     */
	private $consumer;

	/**
     * __construct
     *
	 * @access 		public
     * @param		string		$store		The kind of store to use
     * @return 		void
     */
	public function __construct($store = 'db')
	{
		parent::__construct();

		// Setup the store
		switch (true) {
			case $store == 'db':
				require_once 'Auth/OpenID/MySQLStore.php';
				$this->store = new Auth_OpenID_MySQLStore(Framework_DB::getDBConnection());
				$this->store->createTables();
				break;
		}

		$this->consumer = new Auth_OpenID_Consumer($this->store);

	}

	/**
     * start
     *
     * @access 		public
     * @param		string		$openIDURL		OpenID url to check
     * @return 		mixed
     */
	public function start($openIDURL)
	{
		// Begin the OpenID authentication process.
		$request = $this->consumer->begin($openIDURL);

		if (!$request) {
			//return error
			return PEAR::raiseError('Invalid Request');
		}
		
		return $request;

	}


	/**
     * finish
     *
     * @access 		public
     * @return 		mixed
     */
	public function finish()
	{
		// Complete the authentication process using the server's response.
		$response = $this->consumer->complete();

		return $response;

	}



}

?>